##############################################################
# Purpose:  To display Used and available Memory Sizes
# Author:  Jagadish Manchala
# Description : Displays the Memory graph Of All the java Process Running

##############################################################
# Function for converting the Kilo bytes to Mega Bytes.
converToMB()
{
        echo 'scale=0;'$1'/1024' | bc; 
} 
#=============================================
# JVM status Function starts here
#=============================================
instJvm()
{
       if [ -d "/proc/$1" ] 
       then   
        DATA=`$JAVA_HOME/bin/jstat -gc $1 | grep -v S0C`
          
        EC=`echo $DATA | awk '{print $5}'`
        EU=`echo $DATA | awk '{print $6}'`
        OC=`echo $DATA | awk '{print $7}'`
        OU=`echo $DATA | awk '{print $8}'`
        PC=`echo $DATA | awk '{print $9}'`
        PU=`echo $DATA | awk '{print $10}'`
        echo -e  "$1 $sp3  |" `converToMB $EC`"M   "`converToMB $EU`"M\t| "`converToMB $OC`"M   "`converToMB $OU`"M\t| "`converToMB $PC`"M   "`converToMB $PU`"M"
    fi
}
#=============================================
# main starts here
#=============================================

$JAVA_HOME/bin/jps | cut -d' ' -f1 > /tmp/processIDs
sp3="   "
echo -e "=============================================================="
echo -e " Instance |    Eden    \t|    Old Gen   \t|    Perm Gen"
echo -e "          |Current Used\t| Current Used \t| Current Used"
echo -e "=============================================================="
  
for i in ` cat /tmp/processIDs `
do
        if [ "$i" != ""  ]
        then   
                instJvm $i
        fi     
done
echo -e "=============================================================="
